$(function(){
	var doctorFro  = JSON.parse(sessionStorage.getItem("doctorZz"));
	if(doctorFro==1){
		$("#serchOne").hide();
		$(".make-doctorList").css("margin-top","1rem");
	}else{
		$("#serchTwo").hide();
		$(".make-doctorList").css("margin-top","1.4rem");
	}
	//初始化MUI事件
	mui('.mui-scroll-wrapper').scroll({
		deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
	});
	var realname = utilModule.getQueryString("realname");
	realname = unescape(realname)
	/*获取 疾病列表 接口*/
	doctorList()
	function doctorList(){
		var param;
		var name = $("input[name='name']").val();
		var hos = $("input[name='hosId']").val();
		var clas = $("input[name='clasId']").val();
		var sort = $("input[name='sortId']").val();
		if(name!=""){
			$("#keyword").val(name);
			if(hos==""&&sort==""){
				param= {"pageNo":1,"pageSize":999,"realname":name,"skilful":name};
			}
			if(hos!=""&&sort==""){
				param= {"pageNo":1,"pageSize":999,"realname":name,"skilful":name,"hospitalid":hos};
			}
			if(hos!=""&&sort!=""){
				param= {"pageNo":1,"pageSize":999,"realname":name,"skilful":name,"hospitalid":hos,"orderBy":sort};
			}
			if(hos!=""&&clas!=""&&sort==""){
				param= {"pageNo":1,"pageSize":999,"realname":name,"skilful":name,"hospitalid":hos,"departmentid":clas};
			}
			if(hos!=""&&clas!=""&&sort!=""){
				param= {"pageNo":1,"pageSize":999,"realname":name,"skilful":name,"hospitalid":hos,"departmentid":clas,"orderBy":sort};
			}
			if(hos==""&&sort!=""){
				param= {"pageNo":1,"pageSize":999,"realname":name,"skilful":name,"orderBy":sort};
			}
		}else{
			$("#keyword").val(realname);
			if(hos==""&&sort==""){
				param= {"pageNo":1,"pageSize":999,"realname":realname,"skilful":realname};
			}
			if(hos!=""&&sort==""){
				param= {"pageNo":1,"pageSize":999,"realname":realname,"skilful":realname,"hospitalid":hos};
			}
			if(hos!=""&&sort!=""){
				param= {"pageNo":1,"pageSize":999,"realname":realname,"skilful":realname,"hospitalid":hos,"orderBy":sort};
			}
			if(hos!=""&&clas!=""&&sort==""){
				param= {"pageNo":1,"pageSize":999,"realname":realname,"skilful":realname,"hospitalid":hos,"departmentid":clas};
			}
			if(hos!=""&&clas!=""&&sort!=""){
				param= {"pageNo":1,"pageSize":999,"realname":realname,"skilful":realname,"hospitalid":hos,"departmentid":clas,"orderBy":sort};
			}
			if(hos==""&&sort!=""){
				param= {"pageNo":1,"pageSize":999,"realname":realname,"skilful":realname,"orderBy":sort};
			}
		}
		
//		if(value==undefined){
//			if(sort==undefined){
//				param= {"pageNo":1,"pageSize":999,"realname":realname,"skilful":realname};
//				$("#keyword").val(unescape(realname));
//			}else{
//				param= {"pageNo":1,"pageSize":999,"realname":realname,"skilful":realname,"orderBy":sort};
//				$("#keyword").val(unescape(realname));
//			}
//		}else{
//			$("#keyword").val(value);
//			if(sort==undefined){
//				if(hosId==undefined&&clsId==undefined){
//					param= {"pageNo":1,"pageSize":999,"realname":value,"skilful":value};
//				}
//				if(hosId!=undefined&&clsId==undefined){
//					param= {"pageNo":1,"pageSize":999,"realname":value,"skilful":value,"hospitalid":hosId};	
//				}
//				if(hosId!=undefined&&clsId!=undefined){
//					param= {"pageNo":1,"pageSize":999,"realname":value,"skilful":value,"hospitalid":hosId,"departmentid":clsId};
//				}
//			}else{
//				if(hosId==undefined&&clsId==undefined){
//					param= {"pageNo":1,"pageSize":999,"realname":value,"skilful":value,"orderBy":sort};
//				}
//				if(hosId!=undefined&&clsId==undefined){
//					param= {"pageNo":1,"pageSize":999,"realname":value,"skilful":value,"hospitalid":hosId,"orderBy":sort};	
//				}
//				if(hosId!=undefined&&clsId!=undefined){
//					param= {"pageNo":1,"pageSize":999,"realname":value,"skilful":value,"hospitalid":hosId,"departmentid":clsId,"orderBy":sort};
//				}
//			}
//		}
		
		function callBack(data){
			console.log(data);
			var html2 = "";
			var json=data.doctorList;
			for (j in json) {	
				var $skilful =json[j].skilful;
				if(json[j].skilful==undefined){
					$skilful = " ";
				}
				html2 +='<li class="systemdoctor" v="'+json[j].id+'" m="'+json[j].id+'">'
							+'<div class="goto-doctor">'
								//+'<div class="ask-doctor" p="'+json[j].imprice+'" k="'+json[j].imisopen+'">问医生</div>'
								+'<div class="picture logo">'
									+'<img src="'+json[j].avatar+'">'
								+'</div>'
								+'<b>'+json[j].realname+'<span>'+json[j].deptName+'</span></b>'
								+'<br>'+json[j].doctortitle
								+'<br>'+json[j].hospitalName
								+'<p class="skilled mui-ellipsis font1">擅长:'+$skilful+'</p>'
							+'</div>'
							+'<div class="info font1">'
								+'<div>服务患者端次数:<span class="servicetimes">'+json[j].servicetimes+'</span></div>'
								+'<div>关注人数:<span class="attention-num">'+json[j].concernnumber+'</span></div>'
							+'</div>'
						+'</li>';
			}
			$(".choice-doctorList").empty();
			$(".choice-doctorList").append(html2);	
			
			$(".mui-table-view li").on("click",function(){
				var doctorId=$(this).attr("v");
				location.href = "../doctor/doctor-details.html?doctorid="+doctorId;
			});
			
			
			$(".mui-table-view li .ask-doctor").on("click",function(event){
				event.stopPropagation()
				var result=checkUnfinishedOrder(USERID,$(this).parent().parent().attr("v"));
				if(result==true){
				   if($(this).attr("k")==0){
						mui.alert("该医生暂未开通图文咨询")
					}else{
						var consultOrder={};
						var doctorId = $(this).parent().parent().attr("v");
						consultOrder.doctorid=doctorId;
						consultOrder.payAmount=$(this).attr("p");
						consultOrder.orderDetail="图文咨询";
						consultOrder.orderType=1;
						var orderKey="doctor_index";
						 sessionStorage.setItem(orderKey,JSON.stringify(consultOrder));
						location.href = "../onlineContinuation/medication-consultation.html?doctorId="+doctorId;
					}
				}else{
					return false;
				}
			});
		}
		doctorModule.queryList(param,callBack);
	}
	
	//按医院搜索
	addHospital()
	var hospital = new mui.PopPicker();
	function addHospital(){
		var parma="";
		function callBack(data){
			console.log(data)
			var arr = Array();
			for(i in data){
				arr[i] = {"value":data[i].id,"text":data[i].name}
			}
			console.log(arr)
			$("#hospital").on("click",function(){
				hospital.setData(arr);
				hospital.show(function (selectItems) {
			    	$("#hospital").text(selectItems[0].text);
			    	$("#department").text("科室");
			   		$("input[name='hosId']").val(selectItems[0].value);
			   		$("input[name='clasId']").val("");
			   		doctorList()
			    	addDepartment(selectItems[0].value)
				})
			});
		}
		doctorModule.doctorGethops(parma,callBack);
	}
	
	//按科室搜索
	var hosClass = new mui.PopPicker();
	function addDepartment(hosId){
		var parma="hospitalid="+hosId;
		function callBack(data){
			console.log(data)
			var arr = Array();
			for(i in data){
				arr[i] = {"value":data[i].id,"text":data[i].name}
			}
			console.log(arr)
			$("#department").on("click",function(){
				if($(this).prev().text()=="选择医院"){
					mui.toast("请先选择医院")
				}else{
					hosClass.setData(arr);
					hosClass.show(function (selectItems) {
					    $("#department").text(selectItems[0].text);
					    $("input[name='clasId']").val(selectItems[0].value);
					    doctorList()
					})
				}
			});
		}
		doctorModule.doctorDepartment(parma,callBack);
	}
	
	$(".searchBtn").on("click",function(){
		var value = $("#keyword").val();
		if(value==""){
			mui.alert("请输入内容")
		}else{
			$("input[name='name']").val(value);
			$("input[name='hosId']").val("");
			$("input[name='clasId']").val("");
			$("input[name='sortId']").val("");
			
			$("#hospital").text("选择医院");
			$("#department").text("科室");
			$("#comprehensive").text("综合");
			doctorList()
		}
	})
	
	//排序
	var hosSort = new mui.PopPicker();
	hosSort.setData([{
	    value: "doctortitle",
	    text: "职称"
	}, {
	    value: "servicetimes",
	    text: "服务次数"
	}, {
	    value: "concernnumber",
	    text: "关注人数"
	}])
	
	$("#comprehensive").on("click",function(){
		hosSort.show(function (selectItems) {
		    $("#comprehensive").text(selectItems[0].text);
		    $("input[name='sortId']").val(selectItems[0].value);
		    doctorList()
		})
	});
	
	
})
